package com.excel.read;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.db.ColumnInfoBean;
import com.db.ForeignKey;
import com.db.sql.Node;
import com.db.sql.SqlLeafNode;
import com.db.sql.SqlNode;
import com.excel.comp.HTMLTable;

/**
 * 全局配置 从Excel读取的内容都将放进allTable
 * 
 * @author Administrator
 * 
 */
public class Config {
	/**
	 * 所有表格的存放位置，每一张表格必须有名称，和数据，
	 * 并且第一列和第二列数据不能为空，否则程序将不能正确读取。
	 */
	public static AllTable allTable = new AllTable();
	/**
	 * 存放表單的地方
	 */
	public static HashMap<String, HTMLTable> tableMap = new HashMap<String, HTMLTable>();
	
	
	
	
	/**
	 * 只初始化一次
	 */
	public  static boolean initTableFlag = false;
	public  static boolean initAppFlag = false;
	public  static boolean initForm = false;
	public  static boolean initSQL = false;
	
	/**
	 * 读取表格,
	 * 这里读取两张表格,
	 * @param realPath
	 * Excel的全局路径
	 * @param tableName
	 */
	public static void generateTable(String realPath) {
		if (!initTableFlag) {
			//数据库表
			ExcelConfig excelConfig = new GridExcelConfig();
			excelConfig.tableConfig(realPath);	
			configFK();
			configPK();
			initTableFlag = true;
		}
	}
	
	
	
	
	

	

	public static String[][] getCols(String name, String[] cols) {
		String[][] vs = allTable.get(name).getData();
		String rs[][] = new String[vs.length - 1][cols.length];
		int[] colIndexs = new int[cols.length];
		for (int i = 0; i < cols.length; i++) {
			for (int j = 0; j < vs[0].length; j++) {
				if (vs[0][j].equalsIgnoreCase(cols[i])) {
					colIndexs[i] = j;
					break;
				}
			}

		}

		for (int m = 1; m < vs.length; m++) {
			for (int n = 0; n < cols.length; n++) {
				rs[m - 1][n] = vs[m][colIndexs[n]];
			}

		}
		return rs;
	}
	
	
	public static ColumnInfoBean getPKBean(String tableName) {
		ColumnInfoBean cb = new ColumnInfoBean();
		DBTable dbTable = allTable.get(tableName);
		return dbTable.getPKBean(tableName);
	}
	
	
	
	public static HashMap[] getColumnInfoBeans(String name) {
		HashMap [] infoBeans;
		String[][] vs = allTable.get(name).getData();
		infoBeans = new HashMap[vs.length-1];
		for (int i = 1; i < vs.length; i++) {
			HashMap<String, String> hm = new HashMap();
			infoBeans[i-1] = hm;
			for (int j = 0; j < vs[0].length; j++) {
				String aName = vs[0][j];
				String aValue = vs[i][j];
				hm.put(aName, aValue);
			}

		}
		return infoBeans;
	}
	
	
	/**
	 * 根据表名.字段名取得Ｂean
	 * @param tableName
	 * @return
	 */
	public static ColumnInfoBean getBean(String tableName, String fieldName) {
		ColumnInfoBean bean = null;	
		DBTable dbTable =  allTable.get(tableName);
		bean = 	dbTable.getColumnInfo(fieldName);		
		return bean;
	}
	
	
	
	
	
	
	public static FormTable getFormTable(String tableName) {

		FormTable table = allTable.getForm(tableName);
		return table;
	}
	



	class FK{
		public String tableName;
		public String fieldName;		
	}
	
	public FK getFKStruct(String decodeName) {
		int aIndex = decodeName.indexOf("(");
		String tableName = decodeName.substring(0, aIndex);
		String fieldName = decodeName.substring(aIndex, decodeName.length());
		FK fk = new FK();
		fk.tableName = tableName;
		fk.fieldName = fieldName;
		return fk;
	}
	
	/**
	 * 配置主外键
	 */
	public static void  configFK() {
		String tableNames[] = allTable.getTableNames();
		for(int i=0; i<tableNames.length; i++) {
			String tableName = tableNames[i];
			DBTable dbTable = (DBTable) allTable.get(tableName);
			//取得所有外键
			List fkList = dbTable.getFkList();
			for(int j=0; j<fkList.size(); j++) {
				ForeignKey foreignKey = (ForeignKey) fkList.get(j);				
				DBTable toTable = (DBTable) allTable.get(foreignKey.getToTableName());
				//外键信息设置完全
				foreignKey.setToTable(toTable);
				//添加外键信息用于索引，加快查询速度
				dbTable.putFK(foreignKey.getFkName(), foreignKey);
				//添加反向关联，为1对多关联
				toTable.putReserveFK(foreignKey.getFkName(), foreignKey);
				
			
				
				
			}
		}
	}
	
	
	public static ColumnInfoBean  configPK() {
		String tableNames[] = allTable.getTableNames();
		for(int m=0; m<tableNames.length; m++) {
			String tableName = tableNames[m];
			DBTable dbTable = (DBTable) allTable.get(tableName);			
			String data [][] = dbTable.data;
			for(int i=1; i<data.length; i++) {
				String pkDesc = data[i][dbTable.pkIndex];
				boolean pkFlag = false;
				boolean rpkFlag = false;
				boolean unique = false;
				//双业务主键
				if(pkDesc != null && pkDesc.indexOf("&")>0) {
					String [] pks = pkDesc.split("&");
					if(pks[0].equalsIgnoreCase("PK") || pks[1].equalsIgnoreCase("PK")) {
						pkFlag = true;
					}					
					if(pks[0].equalsIgnoreCase("RPK") || pks[1].equalsIgnoreCase("RPK")) {
						rpkFlag = true;
					}
				}else if(pkDesc != null){
					if(pkDesc.equalsIgnoreCase("RPK")) {
						rpkFlag = true;
					}else if(pkDesc.equalsIgnoreCase("UNIQUE")) {
						unique = true;
					}else if(pkDesc.equalsIgnoreCase("PK")) {
						pkFlag = true;
					}
				}
				if(pkFlag) {
					ColumnInfoBean aBean = dbTable.fieldMap.get(dbTable.fieldNames[i-1]);
					dbTable.pk = aBean;
					dbTable.pks.add(aBean);
				}
				
				if(rpkFlag) {
					ColumnInfoBean aBean = dbTable.fieldMap.get(dbTable.fieldNames[i-1]);
					dbTable.realPK.add(aBean);
				}
				
				if(unique) {
					ColumnInfoBean aBean = dbTable.fieldMap.get(dbTable.fieldNames[i-1]);
					dbTable.setUnique(dbTable.fieldNames[i-1], aBean);
				}
			}
		}
		return null;
	}
	
	
	/**
	 * 取得自关联表的FK
	 */
	public static String getSelfFK(String tableName) {
		String fk = "";
		DBTable dbTable = (DBTable) allTable.get(tableName);
		List<ForeignKey> fkList = dbTable.getFkList();
		for(int i=0; i<fkList.size(); i++) {
			ForeignKey foreignKey = fkList.get(i);
			if(foreignKey.getToTableName().equalsIgnoreCase(tableName)) {
				fk = foreignKey.getFkName();
			}
		}
		return fk;
	}
	
	/**
	 * 取得关联表的FK
	 */
	public static ForeignKey getFK(SqlNode sqlNode, SqlNode sqlNode2) {
		String fk = "";
		String pTableName = sqlNode2.getNodeName();
		DBTable dbTable = (DBTable) allTable.get(sqlNode.getNodeName());
		List<ForeignKey> fkList = dbTable.getFkList();
		for(int i=0; i<fkList.size(); i++) {
			ForeignKey foreignKey = fkList.get(i);
			if(foreignKey.getToTableName().equalsIgnoreCase(pTableName)) {
				return foreignKey;
			}
		}
		return null;
	}
	
	
	/**
	 * 取得关联表的FK
	 */
	public static ForeignKey getFK(String childTable, String parentTable) {
		SqlNode sqlNode =  getSQLNode(childTable);
		SqlNode sqlNode2 = getSQLNode(parentTable);
		String fk = "";
		String pTableName = sqlNode2.getNodeName();
		DBTable dbTable = (DBTable) allTable.get(sqlNode.getNodeName());
		List<ForeignKey> fkList = dbTable.getFkList();
		for(int i=0; i<fkList.size(); i++) {
			ForeignKey foreignKey = fkList.get(i);
			if(foreignKey.getToTableName().equalsIgnoreCase(pTableName)) {
				return foreignKey;
			}
		}
		return null;
	}
	
	public static SqlNode getSQLNode(String tableName) {
		FormTable gst = null;
		AllTable allTable  = Config.allTable;
		gst = allTable.getForm(tableName);
		return gst.getSqlNode();
	}
	
	
	public static SQLTable getSQLTable(String tableName) {
		AllTable allTable  = Config.allTable;
		return allTable.getSQL(tableName);
	}
	
	
	
	public static GridSearchTable getGridSearchTable(String tableName) {
		AllTable allTable  = Config.allTable;
		return allTable.getGrid(tableName);
	}
	
	
	
	public static SqlLeafNode getSQLLeafNode(String searchTableName, String controlName) {
		GridSearchTable gst = null;
		AllTable allTable  = Config.allTable;
		gst = allTable.getGrid("Stuff");
		String[] queryNames = gst.getQueryNameFields();
		Map keyValue  = gst.getFullNameVaue();
		SqlNode sqlNode = gst.getRoot();
		String fullPath = (String) keyValue.get(controlName);
		List<Node> nodes = gst.getSqlNode(fullPath, controlName, sqlNode);
		SqlLeafNode sfn = (SqlLeafNode) nodes.get(nodes.size()-1);
		return sfn;
	}
}
